package com.xs.bookspoststation.UserActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xs.bookspoststation.R;
import com.xs.bookspoststation.adapter.UserAdapter;
import com.xs.bookspoststation.bean.User;
import com.xs.bookspoststation.db.DBHelperUser;
import com.xs.bookspoststation.db.UserBean;

import java.util.ArrayList;
import java.util.List;

public class UserViewActivity extends AppCompatActivity {

    //定义对象
    TextView user_sel_username;
    private ListView listView;
    private Button btn_back2;
    private List<User> userList;
    private Context context;
    private UserBean userdao;
    //定义一个数据库帮助类
    private DBHelperUser dbHelperUser;
    //定义一个数据库操作类
//    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_view);
        //绑定控件
        user_sel_username = (TextView) findViewById(R.id.user_sel_username);

        listView = (ListView)findViewById(R.id.listView1);
        btn_back2 = (Button)findViewById(R.id.btn_back2);
        //返回按钮
        btn_back2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(UserViewActivity.this,OperateUserActivity.class);
                Toast.makeText(getApplicationContext(), "返回上一页",Toast.LENGTH_LONG).show();
                startActivity(intent);
            }
        });
    }

    /**
     * 启动
     */
    @Override
    protected void onStart() {
        super.onStart();
        getData();
        UserAdapter myAdapter=new UserAdapter(this,R.layout.userlist,userList);
        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3) {
                User user = (User)arg0.getItemAtPosition(arg2);
                final String name = user.getUsername();
                final String pass = user.getUserpwd();
                final String userphone = user.getUserphone();
                final String usersex = user.getUsersex();
                final String userhobby = user.getUserhobby();
                final String usereducation = user.getUsereducation();
                final String userfullname = user.getUserfullname();
                final String useremail = user.getUseremail();
                final String userprovince = user.getUserprovince();
                final String userschool = user.getUserschool();
                //弹出框选择删除还是修改
                AlertDialog.Builder builder=new AlertDialog.Builder(UserViewActivity.this);
                builder.setTitle("确定以下操作？？？");
                /**
                 * 修改逻辑代码
                 */
                builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        //启动Activity
                        Intent intent=new Intent(UserViewActivity.this,UpdateUserActivity.class);
                        Bundle bundel=new Bundle();//传入页面数据
                        bundel.putString("name", name);
                        bundel.putString("pass", pass);
                        bundel.putString("userphone",userphone);
                        bundel.putString("usersex",usersex);
                        bundel.putString("userhobby",userhobby);
                        bundel.putString("usereducation",usereducation);
                        bundel.putString("userfullname",userfullname);
                        bundel.putString("useremail",useremail);
                        bundel.putString("userprovince",userprovince);
                        bundel.putString("userschool",userschool);
//                        intent.setClass(getApplicationContext(),UpdateUserActivity.class);
                        intent.putExtras(bundel);
                        startActivity(intent);
                    }});

                /**
                 * 删除逻辑代码
                 */
                builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        //创建user对象
                        User user = new User(name,pass,userphone,usersex,userhobby,usereducation,userfullname,useremail,userprovince,userschool);
                        //打开数据库
                        SQLiteDatabase db = dbHelperUser.getWritableDatabase();
                        if(db.isOpen()){
                            //直接根据用户名判断这个用户是否存在
                            String name = user.getUsername();
                            //调用delete
                            db.delete("book_user","username=?",new String[]{name});
                            //打印日志
                            Log.v("删除用户信息操作","删除用户信息成功！");
                            Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_LONG).show();
                       }else{
                            Toast.makeText(UserViewActivity.this,"数据库打开失败",Toast.LENGTH_LONG).show();
                        }
                        onStart();
                    }});
                builder.show();
            }});
    }

    /**
     * 调用添加方法
     */
    private void getData() {
        dbHelperUser = new DBHelperUser(UserViewActivity.this,"books_station.db",null,1);
        userdao = new UserBean(this);
        userList = userdao.findAll();




//        SQLiteDatabase db = dbHelperUser.getWritableDatabase();
//        List<User> users = null;
//        if (db.isOpen()) {
//            Cursor cursor = db.query("book_user", null, null, null, null, null, null);
//            users = new ArrayList<User>();
//            while (cursor.moveToNext()) {
//                User user = new User();
//                String user_sel_username2 = cursor.getString(cursor.getColumnIndex("username"));
//                user.setUsername(user_sel_username2);
//                String user_sel_password2 = cursor.getString(cursor.getColumnIndex("userpwd"));
//                user.setUserpwd(user_sel_password2);
//                String user_sel_phone2 = cursor.getString(cursor.getColumnIndex("userphone"));
//                user.setUsername(user_sel_phone2);
//                String user_sel_sex3 = cursor.getString(cursor.getColumnIndex("usersex"));
//                user.setUsername(user_sel_sex3);
//                String user_sel_hobby2 = cursor.getString(cursor.getColumnIndex("userhobby"));
//                user.setUsername(user_sel_hobby2);
//                String user_sel_education2 = cursor.getString(cursor.getColumnIndex("usereducation"));
//                user.setUsername(user_sel_education2);
//                String user_sel_email2 = cursor.getString(cursor.getColumnIndex("useremail"));
//                user.setUsername(user_sel_email2);
//                String user_sel_province2 = cursor.getString(cursor.getColumnIndex("userprovince"));
//                user.setUsername(user_sel_province2);
//                String user_sel_school2 = cursor.getString(cursor.getColumnIndex("userschool"));
//                user.setUsername(user_sel_school2);
//                String user_sel_fullname2 = cursor.getString(cursor.getColumnIndex("userfullname"));
//                user.setUsername(user_sel_fullname2);
//                users.add(user);
//            }
//            cursor.close();
//            db.close();
//        }
    }
}
